<?php
namespace app\adminapiv3\controller;

/**
 * @name: 主机接口
 * @Param: 
 * @Description: 
 * @Author: foo
 * @Date: 2019-10-22 16:55:56
 */
use app\adminapiv3\service;
class Host extends Base
{
    /**
     * @name: 数据统计
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-23 13:22:47
     */
    function dataStatistics ()
    {
        $Host = new service\Host();

        $data = $this->toDo([$Host, 'statistics']);

        $this->jsonOutPut($data);
    }

    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-24 15:26:39
     */
    function list ()
    {
        $filterData['alias'] = $this->filterInput('alias','post');//主机名
        $filterData['os'] = $this->filterInput('os', 'post'); //系统名
        $filterData['ip'] = $this->filterInput('ip', 'post');//ip
        $filterData['description'] = $this->filterInput('description', 'post');//备注


        $Host = new service\Host();
        $data = $Host->list($filterData, $this->getLimit());
        $this->jsonOutPut($data);

    }

    /**
     * @name: 主机详情
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-25 10:55:09
     */
    function detail ($host_id)
    {
        $Host = new service\Host();

        $data = $this->toDo([$Host,'detail'], [$host_id]);

        $this->jsonOutPut($data, true);
        $this->Async('Client', 'MachineInfo');
    }

        /**
     * @name: 添加主机信息 
     * @Param: 
     *  GET:
     *  POST: name:主机名; alias:主机别名;  ipAddress:主机ip地址; software: 业务系统名; os:系统id 1windows 2linux; ministry:所属部门名; manager:管理员名;
     * @Description: 
     * @Author: foo
     * @Date: 2019-03-22 17:59:09
     */
    function add () 
    {
        $hostData['name'] = $this->filterInput('name*','post'); //主机名
        $hostData['tag'] = $this->filterInput('tag*', 'post', 7);//标签id
        $hostData['software_id'] = $this->filterInput('software_id*','post');//所属业务系统名
        $hostData['ministry_id'] = $this->filterInput('ministry_id','post');//所属部门id
        $hostData['manager_id'] = $this->filterInput('manager_id','post');//安全责任人id
        $hostData['develop_provider_id'] = $this->filterInput('develop_provider_id','post');//系统开发尚商id
        $hostData['maintain_provider_id'] = $this->filterInput('maintain_provider_id','post');//运维服务商id
        $hostData['safe_provider_id'] = $this->filterInput('safe_provider_id','post');//安全服务商id
        $hostData['address_id'] = $this->filterInput('address_id','post');//资产位置id
//ssh信息
        $hostData['ip'] = $contactData['ip'] = $this->filterInput('ip*','post');//IP地址
        $contactData['type'] = $this->filterInput('type*','post');//安装方式 1插件 2ssh
        if ($contactData['type'] == 2)
        {
            $contactData['user'] = $this->filterInput('user*','post');//ssh用户名
            $contactData['pwd'] = $this->filterInput('pwd*','post');//ssh密码
            $contactData['port'] = $this->filterInput('port*','post');//ssh端口
        }
        

        $Host = new service\Host();
        $this->toDo([$Host, 'add'], [$hostData, $contactData]);

        $this->jsonOutPut();
        
    }

    /**
     * @name: 单个/批量 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 13:16:46
     */
    function delete ()
    {
        $host_id = $this->filterInput('host_id*','post'); //主机id

        $Host = new service\Host();
        $osHost = $Host->uninstall($host_id);
        $this->toDo('resetOnlineHost', [$osHost]);
        $this->jsonOutPut([], true);
        $this->Async('Client', 'Uninsatll');
    }

    /**
     * @name: 详情
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 13:28:36
     */
    function info ($host_id)
    {
        $Host = new service\Host();
        $data = $this->toDo([$Host, 'info'], [$host_id]);

        $this->jsonOutPut($data, true);
        $this->Async('Client', 'MachineInfo');
    }

    /**
     * @name: 主机硬件使用
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 14:25:03
     */
    function hardwareUsed ($host_id)
    {
        $Host = new service\Host();
        $data = $this->toDo([$Host, 'hardware'], [$host_id]);

        $this->jsonOutPut($data, true);
        $this->Async('Client', 'MachineInfo');
    }

    /**
     * @name: 检测
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 15:11:23
     */
    function checkClient ()
    {
        $contactData['ip'] = $this->filterInput('ip*', 'post');//IP地址
        $contactData['type'] = $this->filterInput('type*', 'post');//类型

        if($contactData['type'] == 2)
        {
            $contactData['user'] = $this->filterInput('user*', 'post');//用户名
            $contactData['password'] = $this->filterInput('pwd*', 'post');//密码
            $contactData['port'] = $this->filterInput('port*', 'post');//端口号
        }
        

        $Host = new service\Host();
        $Host->toDo('checkClient', [$contactData]);

        $this->jsonOutPut();
    }
    /**
     * @name: 筛选项
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-30 10:34:47
     */

    function filterData ()
    {
        $Host = new service\Host();
        $data = $Host->filterData();

        $this->jsonOutPut($data);
    }

    /**
     * @name: 端口列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 15:00:56
     */
    function portList ($host_id)
    {
        $filterData['port'] = $this->filterInput('port');//端口
        $filterData['protocols'] = $this->filterInput('protocols');//协议
        $filterData['guise'] = $this->filterInput('guise');//是否伪装端口
        $filterData['host_id'] = $host_id;
        
        $HostPort = new service\Host();
        $data = $HostPort->toDo('portList',[$filterData, $this->getLimit()]);

        $this->jsonOutPut($data, true);
        $this->Async('Client', 'Ports');
    }

    /**
     * @name: 重新安装
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-15 15:50:53
     */
    function reinstall ($host_id)
    {
        $Host = new service\Host();
        $Host->toDo('reinstall');

        $this->jsonOutPut();
    }

    /**
     * @name 清空黑名单
     * @author wx
     * @date 2020/4/26 14:39
     */
    function clearBlack()
    {
        global $logContent;
        $logContent = '清空黑名单';

        $host_id = $this->filterInput('host_id*');

        $server = new service\Base();
        $server->checkOptionHost($host_id);

        //发送命令给老板
        $this->jsonOutPut([], true);

        $this->Async('Wipf', 'Webcmd', ['action' => 'ClearBlackList']);
    }

    /**
     * @name 开始文件查杀
     * @author wx
     * @date 2020/4/26 16:20
     */
    function fileKillStart()
    {
        global $logContent;
        $logContent = '文件查杀开始';

        $hostId = $this->filterInput('host_id*');

        $Host = new service\Host();
        $Host->fillKillStart($hostId);

        $this->jsonOutPut([], true);

        //通知中间件
        $this->Async('Defense','ScanCmd', ['status' => 2]);
    }

    /**
     * @name 停止文件查杀
     * @author wx
     * @date 2020/4/26 16:55
     */
    function fileKillStop()
    {
        global $logContent;
        $logContent = '文件查杀停止';

        $hostId = $this->filterInput('host_id*');

        $Host = new service\Host();
        $Host->fillKillStop($hostId);

        $this->jsonOutPut([], true);

        //通知中间件
        $this->Async('Defense','ScanCmd', ['status' => 0]);
    }

    /**
     * @name 文件查杀进度
     * @Author wx
     * @date 2019/8/14 15:25
     */
    public function fileKillRunStatus()
    {
        $hostId = $this->filterInput('host_id*','post');//id

        $service = new service\Host();
        $data = $service->fileKillRunStatus($hostId);

        $this->jsonOutPut($data);
    }

    /**
     * @name 运行状态
     * @Author wx
     * @date 2019/8/14 14:08
     */
    public function runStatus($host_id)
    {
        $connect_id = $this->filterInput('runstatus_id*','post');//会话id

        $service = new service\Host();
        $info = $service->runStatus($host_id, $connect_id);

        $this->jsonOutPut($info, true);

        $this->Async('Client', 'MachineInfo');
    }

    /**
     * @name 主机详情 ->功能设置
     * @param $host_id
     * @author wx
     * @date 2020/5/10 10:16
     */
   public function hostInfo($host_id)
   {
       $service = new service\Host();
       $info = $service->hostInfo($host_id);

       $this->jsonOutPut($info);
   }

    /**
     * @name 网络防御 ->功能设置 -> 修改
     * @param $host_id
     * @author wx
     * @date 2020/5/10 10:37
     */
    public function wipfModify($host_id)
    {
        global $logContent;
        $logContent = '修改网络防御配置';

        $ipsConfig['status'] = $this->filterInput('status*');//状态
        $ipsConfig['black'] = $this->filterInput('black*','post');//黑名单
        $ipsConfig['type'] = $this->filterInput('type');//类型
        $ipsConfig['protect_ip'] = $this->filterInput('protect_ip');//保护ip
        $ipsConfig['device_ip'] = $this->filterInput('device_ip');//ids ip
        $ipsConfig['firewall'] = $this->filterInput('firewall');//防火墙
        $ipsConfig['csrf'] = $this->filterInput('csrf');//CSRF防御


        $service = new service\Host();
        $service->ipsConfigModify($host_id, $ipsConfig);

        $this->jsonOutPut([], true);

        $this->Async('Wipf', 'Config');
        $this->Async('Wipf', 'WipfRules');
    }

    /**
     * @name 主动防御 ->功能设置 ->修改
     * @param $host_id
     * @author wx
     * @date 2020/6/24 9:21
     */
    public function defenseModify($host_id)
    {
        global $logContent;
        $logContent = '修改主动防御配置';

        $hostStatus['auto_defense'] = $this->filterInput('auto_defense*','post');//主动防御
        $hostStatus['shell_defense'] = $this->filterInput('shell_defense*','post');//脚本防御
        $hostStatus['shell_defense_handle'] = $this->filterInput('shell_defense_handle*','post');//脚本处理模式
        $hostStatus['shell_defense_level'] = $this->filterInput('shell_defense_level*','post');//敏感度
        $hostStatus['illegal_modify'] = $this->filterInput('illegal_modify*','post');//防篡改
        $hostStatus['file_safe'] = $this->filterInput('file_safe*','post');//畸形文件保护
        $hostStatus['empowerment'] = $this->filterInput('empowerment*','post');//防提权

        $service = new service\Host();
        $service->hostStatusModify($host_id, $hostStatus);

        $this->jsonOutPut([], true);

        $this->Async('Defense', 'Config');
    }

    /**
     * @name 主机备注修改
     * @param $host_id
     * @author wx
     * @date 2020/6/24 9:30
     */
    public function descriptionModify($host_id)
    {
        global $logContent;
        $logContent = '修改主机备注';

        $host['description'] = $this->filterInput('description','post');//备注

        $service = new service\Host();
        $service->descritionModify($host_id, $host);

        $this->jsonOutPut();


    }

    /**
     * @name 客户端安装包下载
     * @author wx
     * @date 2020/7/14 13:12
     */
    public function clientDownload()
    {
        $type = $this->filterInput('type', 'post');


        if (!in_array($type, array('linux', 'windows'))) {
            jsonErrorOut('参数错误', 202);
        }

        if ($type == 'linux') {
            $installName = 'static/InstallPackage/linux/install.sh';
        }else{
            $installName = 'static/InstallPackage/windows/install.exe';
        }

        if (!file_exists($installName)) {
            jsonErrorOut('安装包不存在', 202);
        }

        $server = new service\Host();
        $bind_key = $server->clientDownload();

        $data['ip'] = $_SERVER['SERVER_ADDR'];
        $data['port'] = $_SERVER['SERVER_PORT'];
        $data['url'] = $_SERVER['REQUEST_SCHEME'] . '://'. $_SERVER['HTTP_HOST'].'/'.$installName;
        $data['bind_key'] = $bind_key;

        $this->jsonOutPut($data);
    }



}